package com.dhu.demo.controller;

import com.dhu.demo.entity.Student;
import com.dhu.demo.entity.StudentWithScore;
import com.dhu.demo.service.ScService;
import com.dhu.demo.service.StudentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/student")
@RestController
@CrossOrigin("*")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/findAllStudent")
    private List<Student> getStudent(){
        return studentService.findAll();
    }

   @RequestMapping("/insertStudent")
    private int insertStudent(@RequestBody Student student){
        int studentnumber=student.getStudentnumber();
        String department=student.getDepartment();
        String major=student.getMajor();
        String classname=student.getStudentname();
        String studentname=student.getStudentname();
        Student record=new Student(studentnumber,department,major,classname,studentname);

        return studentService.insert(record);
    }

    @RequestMapping("/updateStudent")
    private int updateStudent(@RequestBody Student student){
        int studentnumber=student.getStudentnumber();
        String department=student.getDepartment();
        String major=student.getMajor();
        String classname=student.getStudentname();
        String studentname=student.getStudentname();
        Student record=new Student(studentnumber,department,major,classname,studentname);
        return studentService.updateByPrimaryKey(record);
    }

    @RequestMapping("/deleteStudent")
    private int deleteInfo(@RequestBody Student student){
        ScService scService=new ScService();
        int studentNumber=student.getStudentnumber();
        scService.deleteAllInfoByStudentnumber(studentNumber);
        return studentService.deleteByPrimaryKey(studentNumber);
    }

    //添加查询该用户所有信息并返回的功能
    @RequestMapping("/getStudentInfo")
    private Student getStudentInfo(@Param("studentnumber") Integer studentnumber){
        return studentService.selectStudnetInfoByStudentNumber(studentnumber);
    }
}
